import prettier from 'prettier';
import parserBabel from 'prettier/parser-babel';
import parserPostCss from 'prettier/parser-postcss';
import parserHtml from 'prettier/parser-html';

const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue'];

const factory = (content: string, fileType: string) => {
  let parser: prettier.BuiltInParserName | any;
  if (fileType === 'js' || fileType === 'jsx' || fileType === 'ts' || fileType === 'tsx') {
    parser = 'babel-ts';
  } else if (fileType === 'json') {
    parser = 'json-stringify';
  } else if (PARSERS.indexOf(fileType) >= 0) {
    parser = fileType;
  } else {
    return content;
  }
  return prettier.format(content, {
    parser,
    plugins: [parserBabel, parserPostCss, parserHtml],
    singleQuote: true,
    jsxSingleQuote: false,
  });
};

export default factory;
